from common.Utils import SHPDriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time

# 商城个人中心页面
class ShoppingBackground:
    def __init__(self):
        self.url = "http://49.235.61.184/"
        self.driver = SHPDriver.driver
        self.driver.get(self.url)
    def personal_centerTest1(self):
        #1.登录
        SHPDriver.Auto_Login()
        # 2. 点击个人中心（添加显式等待）
        personal_center_btn = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//*[@class=\"am-vertical-align-middle\"]"))
        )
        personal_center_btn.click()
        time.sleep(1)

        # 3. 断言是否进入个人中心
        url = self.driver.current_url
        assert url == "http://49.235.61.184/?s=user/index.html", f"未进入个人中心，当前URL：{url}"

        # 4. 验证头像显示
        avatar = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.XPATH, "//*[@class=\"avatar\"]"))
        )
        assert avatar.is_displayed(), "头像未显示"

        # 5. 点击"修改头像"→取消
        modify_avatar_btn = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div[1]/div[4]/div[3]/div/div[1]/div[1]/div[2]/a[1]/span"))
        )
        modify_avatar_btn.click()

        cancel_btn = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//*[@class=\"am-btn am-btn-warning am-radius am-btn-xs am-margin-right-lg\"]"))
        )
        cancel_btn.click()

        # 6. 点击"修改资料"→修改手机（错误模拟）
        modify_info_btn = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div[1]/div[4]/div[3]/div/div[1]/div[1]/div[2]/a[2]/span"))
        )
        modify_info_btn.click()

        modify_phone_btn = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div[1]/div[3]/div[3]/div/dl/dd[6]/span[2]/a"))
        )
        modify_phone_btn.click()

        # 输入错误手机号
        phone_input = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "#accounts"))
        )
        phone_input.send_keys("12312123132321")

        # 获取验证码（模拟）
        get_code_btn = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//*[@data-text=\"获取验证码\"]"))
        )
        get_code_btn.click()

        # 输入错误验证码
        code_input = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//*[@name=\"verify\"]"))
        )
        code_input.send_keys("2222")

        # 点击确认
        confirm_btn = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//*[@class=\"am-btn am-btn-primary am-radius btn-loading-example am-btn-sm\"]"))
        )
        confirm_btn.click()
        time.sleep(1)
        # 截图
        SHPDriver.GetScreenShot("修改手机错误操作")
        # 返回
        self.driver.refresh()
        time.sleep(2.5)



# 执行测试
PersonalCenter = ShoppingBackground()
PersonalCenter.personal_centerTest1()